<!-- 储存基础控件 -->
<template>
  <!-- 单行文本 -->
  <Text
    v-if="code === 'TEXT'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </Text>

  <!-- 多行文本 -->
  <TextArea
    v-if="code === 'TEXT_AREA'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </TextArea>

  <!-- 数字 -->
  <Number
    v-if="code === 'TEXT_NUMBER'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </Number>

  <!-- 日期 -->
  <Date
    v-if="code === 'DATE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </Date>

  <!-- 日期（周） -->
  <DateWeek
    v-if="code === 'DATE_WEEK'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </DateWeek>

  <!-- 日期（月） -->
  <DateMonth
    v-if="code === 'DATE_MONTH'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </DateMonth>

  <!-- 日期（年） -->
  <DateYear
    v-if="code === 'DATE_YEAR'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </DateYear>

  <!-- 日期（季度） -->
  <DateQuarter
    v-if="code === 'DATE_QUARTER'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </DateQuarter>

  <!-- 日期范围 -->
  <DateRange
    v-if="code === 'DATE_RANGE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </DateRange>

  <!-- 下拉选择 -->
  <Select
    v-if="code === 'SELECT'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </Select>

  <!-- 单选框 -->
  <Select
    v-if="code === 'RADIO'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </Select>

  <!-- 多选框 -->
  <CheckBox
    v-if="code === 'CHECK_BOX'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </CheckBox>

  <!-- 单文件上传 -->
  <SingleFile
    v-if="code === 'SINGLE_FILE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </SingleFile>

  <!-- 多文件上传 -->
  <MultiFile
    v-if="code === 'MULTI_FILE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </MultiFile>

  <!-- 单图片上传 -->
  <SinglePicture
    v-if="code === 'SINGLE_PICTURE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </SinglePicture>

  <!-- 多图片上传 -->
  <MultiPicture
    v-if="code === 'MULTI_PICTURE'"
    :props="getProps"
    :name="name"
    :required="required"
    v-model:one-value="oneValue"
    :regex="regex"
    :disabled="disabled"
  >
  </MultiPicture>
</template>

<script setup lang="ts">
const props = withDefaults(
  //参数说明
  defineProps<{
    componentType: string;
    code: string;
    props: { [propName: string]: any };
    name?: string;
    required: boolean;
    values: any;
    disabled?: boolean;
    regex?: {
      [name: string]: {
        role: any;
        name: any;
      };
    };
  }>(),
  {}
);

// console.log(props);

const emit = defineEmits(['update:values']);
//复制一份props
const getProps = computed(() => {
  return props.props;
});

const validator = (value: any, callback: any) => {
  const { componentType, name, values, required, regex } = props;
  if (componentType === 'TEXT') {
    //单行输入框
    const { validate } = getProps.value;
    if (required && !values[0]) return callback('请输入' + name);
    if (!validate) return callback();

    if (regex) {
      const roleItem = regex[validate];
      const checkVal = roleItem.role.test(values[0]);
      if (!checkVal) {
        return callback(new Error('请输入正确的' + roleItem.name));
      }
    }
  } else if (componentType === 'TEXT_AREA') {
    //多行输入框
    if (required && !values[0]) return callback(`请输入${name}`);
  } else if (componentType === 'NUMBER') {
    if (required && !values[0]) return callback(`请输入${name}`);
  } else if (componentType === 'DATE' || componentType === 'DATE_RANGE') {
    if (required && !values[0]) return callback(`请选择日期`);
  }
  callback();
};
//当value只用到第一个绑定这个参数
const oneValue = computed({
  get() {
    return props.values;
  },
  set(val) {
    emit('update:values', val);
  }
});

// 多选双向绑定
const checkGroupValue = computed({
  get() {
    return props.values;
  },
  set(val) {
    emit('update:values', val);
  }
});
</script>

<style></style>
